package com.uah;

import java.util.List;
import java.util.Map;

import com.scraper.CollegeEngineeringWebData;
import com.scraper.CollegeEngineeringWebScraper;
import com.scraper.Contact;
import com.scraper.Link;
import com.scraper.ScraperType;
import com.scraper.WebReader;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.AdapterView.OnItemSelectedListener;

public class CollegesPage2 extends Activity {
	TableLayout page2TableLayout;
	List<Link> collegeListingsLinks;
	ProgressDialog progDialog;
	Spinner collegesMenu;
	List<String> aboutUsParagraphs;
	Map<String, List<Link>> academicDepartments;
	List<Link> researchCenters;
	Map<String, List<Contact>> facultyStaffInfo;
	BackgroundThread scraperThread;
	InitialBackgroundThread loaderThread;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		loaderThread = new InitialBackgroundThread();
		loaderThread.start();
		
		if (loaderThread.isAlive())
		{
			progDialog = ProgressDialog.show(this, "Loading",
		            "....please wait....", true);	
		}
		
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.page1);
	    
	    page2TableLayout = (TableLayout) findViewById(R.id.page1TableLayout);
	    page2TableLayout.removeAllViewsInLayout();
	   
	    collegesMenu = (Spinner)findViewById(R.id.spinner1);	 
	    
	    // get string arrays from resources and add to spinner
	    String[] menuOptions = getResources().getStringArray(R.array.college_page_array);
	    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, menuOptions);
	    collegesMenu.setAdapter(spinnerArrayAdapter);
	    collegesMenu.setOnItemSelectedListener(menuListener);
	}
	
	// When spinner item is selected, populate the layout appropriately
    private  OnItemSelectedListener menuListener = new OnItemSelectedListener(){
    	@Override
        public void onItemSelected(AdapterView<?> parent, View view, 
                int pos, long id) {    	
    		page2TableLayout.removeAllViewsInLayout();
    		try {
    			loaderThread.join();
      	    } catch (InterruptedException e) {
      	    
      	    }
    		
    		// Non default menu items get data from a second background thread, ensure it is finished running
    		if(id > 0)
    		{
    			if (scraperThread.isAlive())
        		{
        			progDialog = ProgressDialog.show(view.getContext(), "Loading",
        		            "....please wait....", true);	
        		}
    			
    			try {
        			scraperThread.join();
          	    } catch (InterruptedException e) {
          	    
          	    }
    		}
    		
            if (id == 0)		//About
            {
            	for(String paragraph : aboutUsParagraphs)
            	{
            		DynamicLayoutCreation.createNewRowWrapping(paragraph, page2TableLayout, view.getContext());
            		DynamicLayoutCreation.createNewRow("", page2TableLayout, view.getContext());
            	}
            	
            }
            else if (id == 1)	//Departments
            {            	
            	DynamicLayoutCreation.createNewRow(academicDepartments, page2TableLayout, view.getContext());
            }
            else if (id == 2)	//Research
            {
            	//TODO: Make the text wrap on these buttons
            	for(Link research : researchCenters)
            	{
            		DynamicLayoutCreation.createNewRow(research, "", page2TableLayout, view.getContext());
            	}
            	
            }
            else if (id == 3)	//Faculty and Stuff
            {     	
            	DynamicLayoutCreation.createNewRow(facultyStaffInfo, "", page2TableLayout, view.getContext());
            }
            else if (id == 4)	//Contact Info
            {
            	DynamicLayoutCreation.createNewRow("Contact Info", page2TableLayout, view.getContext());
            }
            else
            {

            }
        }

    	@Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
	
	 public class BackgroundThread extends Thread
	    {
	    	public void run() {  
	    		android.os.Process.setThreadPriority
	    		(android.os.Process.THREAD_PRIORITY_BACKGROUND);

	    		// Scraping College of Engineering 
	    		CollegeEngineeringWebScraper engineeringScraper = new CollegeEngineeringWebScraper();
	    		WebReader webReader = new WebReader(engineeringScraper);
	    		    	
	        	// Scrape Academics
	        	engineeringScraper.setScraperType(ScraperType.CollegeEngineeringAcademics);
	    		webReader.scrapeWebPage(engineeringScraper.academicsUrl);
	    		CollegeEngineeringWebData collegeData = engineeringScraper.webData; 
	    		academicDepartments = collegeData.academicDepartmentDegrees;
	    		
	    		// Scrape Research Centers 
	    		engineeringScraper.setScraperType(ScraperType.CollegeEngineeringResearchCenters);
	    		webReader.scrapeWebPage(engineeringScraper.researchCentersUrl);
	    		collegeData = engineeringScraper.webData; 
	    		researchCenters = collegeData.researchCenters;
	    		
	    		// Scrape Faculty Staff
	    		engineeringScraper.setScraperType(ScraperType.CollegeEngineeringFacultyStaff);
	    		webReader.scrapeWebPage(engineeringScraper.facultyStaffUrl);
	    		collegeData = engineeringScraper.webData; 
	    		facultyStaffInfo = collegeData.facultyStaffContactInfo;
	    		//Insert work to be done
	        	
	    		if (progDialog.isShowing())
	    			progDialog.dismiss();
	    	}
	    }
	 
	 public class InitialBackgroundThread extends Thread
	    {
	    	public void run() {  
	    		android.os.Process.setThreadPriority
	    		(android.os.Process.THREAD_PRIORITY_BACKGROUND);
	    		
	    		
	    		// Scraping College of Engineering
	    		CollegeEngineeringWebScraper engineeringScraper = new CollegeEngineeringWebScraper();
	    		WebReader webReader = new WebReader(engineeringScraper);
	    		
	    		// Scrape About Us 
	    		engineeringScraper.setScraperType(ScraperType.CollegeEngineeringAboutUs);
	    		try{	//error checking, null pointer exceptions have been occurring
	    			webReader.scrapeWebPage(engineeringScraper.aboutUsUrl); 
		    		}catch(Exception ex){
		    			String url = "http://www.uah.edu/eng/about/about-us";
		        		webReader.scrapeWebPage(url);
		        	}
	    		CollegeEngineeringWebData collegeData = engineeringScraper.webData; 
	    		aboutUsParagraphs = collegeData.aboutUsParagraphs; 
	        	
	    		if (progDialog.isShowing())
	    			progDialog.dismiss();
	    		
	    		scraperThread = new BackgroundThread();
	    		scraperThread.start();
	    	}
	    }
	 
}
